#pragma once

#include <string>
#include <unordered_map>

#include <rtc_base/rtc_certificate.h>

#include "ice/port_allocator.h"
#include "base/event_loop.h"
#include "base/conf.h"

extern xrtc::GeneralConf* g_conf;

namespace xrtc {

class PushStream;    

class RtcStreamManager {
public:
    RtcStreamManager(EventLoop* el) : m_el(el), m_allocator(new PortAllocator()) {
        m_allocator->set_port_range(g_conf->ice_min_port, g_conf->ice_max_port);
    }

    ~RtcStreamManager() = default;

    // 查询流
    PushStream* findPushStream(const std::string& stream_name);

    // 创建推流, 如果已存在一路流，则先关闭再创建
    int createPushStream(uint64_t uid, const std::string& stream_name, 
                        bool audio, bool video, uint32_t log_id, 
                        rtc::RTCCertificate* certificate, std::string& offer);
    
    // 处理客户端发送的answer
    int set_answer(uint64_t uid, const std::string& stream_name,
            const std::string& answer, const std::string& stream_type, 
            uint32_t log_id);

private:
    EventLoop* m_el;
    // key : stream_name, value : PushStream*
    std::unordered_map<std::string, PushStream*> m_push_streams; 
    std::unique_ptr<PortAllocator> m_allocator;
};

} // namespace xrtc
